<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Payconfig;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
    	echo md5(md5('123456') . 'ehrKud');
    	die;
//        $alipay = load_ali();
//        var_dump($alipay);
//        echo '<hr>';
//        $payconfig = Payconfig::search(['merid'=>101],['limit'=>1]);
//        $config = [
//            'token' => '',
//            'appid' => $payconfig['wec_appid'],
//            'appsecret' => $payconfig['wec_appsecret'],
//            'encodingaeskey' => '',
//            // 配置商户支付参数（可选，在使用支付功能时需要）
//            'mch_id' => $payconfig['wec_mchid'],
//            'mch_key' => $payconfig['wec_key'],
//            // 配置商户支付双向证书目录（可选，在使用退款|打款|红包时需要）
//            'ssl_key' => '',
//            'ssl_cer' => '',
//            // 缓存目录配置（可选，需拥有读写权限）
//            'cache_path' => '',
//        ];
//        $wechat = load_wec('Pay',$config);
//        var_dump($wechat);
//        echo '<hr>';
        return $this->view->fetch();
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'http://www.fastadmin.net?ref=news']);
    }

    //导入壹壹会的商品
    public function importGoods(){
    	set_time_limit(0);
    	$goods = Db::table('mall_goods')
		    ->field('name as goods_name, info as content, nums as sale_number, totals as store_count, bprice as market_price, price as shop_price, in_price as cost_price, pic as original_image, pic1, pic2, pic3, pic4, pic5, cate as category_id, shopid as business_id, ishot as is_hot, isnew as is_new, isrec as is_recommend, sendpoint as give_integral, sendmoney as give_balance, usepoint as use_point, add_days as add_days')
		    ->select();
    	if($goods) {
    		foreach ($goods as $good) {
    			$images = [$good['pic1'], $good['pic2'], $good['pic3'], $good['pic4'], $good['pic5']];
    			unset($good['pic1']);
			    unset($good['pic2']);
			    unset($good['pic3']);
			    unset($good['pic4']);
			    unset($good['pic5']);
			    $good['merid'] = 2;
			    
			    $goodsId = Db::name('goods')->insertGetId($good);
			    $images = array_filter($images);
			    if($images) {
			    	foreach ($images as $image) {
			    		$img = [
			    			'goods_id' => $goodsId,
			    		    'image_url' => $image,
					    ];
			    		Db::name('goodsImages')->insert($img);
				    }
			    }
		    }
	    }

    }
	
	
	//导入壹壹会的分类
    public function importCate(){
    	$cate = Db::table('mall_goods_cate')->select();
    	$datas = [];
    	foreach($cate as $v) {
    	    $datas[] = [
    	    	'id' => $v['id'],
		        'parent_id' => $v['pid'],
		        'name' => $v['name'],
		        'image' => $v['pic'],
		        'create_time' => time(),
		        'update_time' => time()
	        ];
	    }
	    
	    $res = Db::name('goodsCategory')->insertAll($datas);
    	dump($res);
    }
	
	
	//导入壹壹会的商家
    public function importShop(){
	    $cate = Db::table('mall_user_shop')->select();
	    $datas = [];
	    foreach($cate as $v) {
		    $datas[] = [
			    'id' => $v['shopid'],
			    'name' => $v['shopname'],
			    'username' => $v['shopname'],
		    ];
	    }
	
	    $res = Db::name('business')->insertAll($datas);
	    dump($res);
    }
    
    
}
